﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows;

namespace AI.EditMode
{
    public class AddNode : iEditMode
    {

        Point _SPoint = null;
        Point _EPoint = null;
        Node _CurNode = null;


        Pen _LinkPen = new Pen(Color.RosyBrown, 3f);
        
        

        public AddNode(AIInfo Form)
            : base(Form)
        {

            foreach (Node item in _Form._MIM._BaseNode._NodeList.Values)
            {
                _Form.listBox2.Items.Add(item);

            }

            _Form.addNode.Click += new EventHandler(addNode_Click);
            _Form.delNode.Click += new EventHandler(delNode_Click);

            _Form.listBox2.SelectedIndexChanged += new EventHandler(listBox2_SelectedIndexChanged);
            _Form.btnReCaculate.Click += new EventHandler(btnReCaculate_Click);
        }

        void btnReCaculate_Click(object sender, EventArgs e)
        {
            _Form._MIM._BaseNode.ReCaculate();
        }

        void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            _CurNode = _Form.listBox2.SelectedItem as Node;
            _Form.propertyGrid2.SelectedObject = _CurNode;

            //_SPoint = _Form._MIM._BaseNode._PointList[_CurNode._SIndex];
            //_EPoint = _Form._MIM._BaseNode._PointList[_CurNode._EIndex];

        }

        void delNode_Click(object sender, EventArgs e)
        {
            if (_SPoint == null || _EPoint == null)
                return;


            List<Node> list = _Form._MIM._BaseNode.RemoveNode(_SPoint._index, _EPoint._index);

            foreach (Node item in list)
            {
                _Form.listBox2.Items.Remove( item);
            }

        }

        void addNode_Click(object sender, EventArgs e)
        {
            if (_SPoint == null || _EPoint == null)
                return;

            List<Node> nodeList = _Form._MIM._BaseNode.AddNode(_SPoint._index, _EPoint._index);

            foreach(Node node in nodeList)
            {
                _Form.listBox2.Items.Add(node);
            }
            
            
            

        }

        public override void MouseClick(MouseButtons button,double x,double y)
        {
            if (button == MouseButtons.Right)
            {
                _EPoint = _Form._MIM._BaseNode.GetPoint(x, y);
                _Form.E_Info.Text = _EPoint.ToString();

            }
            else if (button == MouseButtons.Left)
            {
                _SPoint = _Form._MIM._BaseNode.GetPoint(x, y);
                _Form.S_Info.Text = _SPoint.ToString();
            }
        }

        void Draw(Graphics g, Pen pen, Node node)
        {
            Point s = _Form._MIM._BaseNode._PointList[node._SIndex];
            Point e = _Form._MIM._BaseNode._PointList[node._EIndex];

            g.DrawLine(pen, (float)s._GPSPos.X, (float)s._GPSPos.Y, (float)e._GPSPos.X, (float)e._GPSPos.Y);

        }

        public override void Draw(Graphics g)
        {
            if (_CurNode != null)
            {
            //    Draw(g, _Cur_Pen, _CurNode);

                _Form.DrawLine(_Cur_Pen, _Form._MIM._BaseNode._PointList[_CurNode._SIndex]._GPSPos, _Form._MIM._BaseNode._PointList[_CurNode._EIndex]._GPSPos);

                foreach (int nodeIndex in _CurNode._NeighborList)
                {
                    Node node = _Form._MIM._BaseNode._NodeList[ nodeIndex];
                    _Form.DrawLine(_LinkPen, _Form._MIM._BaseNode._PointList[node._SIndex]._GPSPos, _Form._MIM._BaseNode._PointList[node._EIndex]._GPSPos);
//                    Draw(g,_LinkPen, node);
                }
            }
                
        }


    }
}
